#! /bin/bash

# This script make both full and incremental backups for a given directory.
# The output file will have the following format:
#        directory_name.date.f.tarz       in case of full backup
#        directory_name.date.i.tarz       in case of incremental backup
#-------------------------------------------------------------------------
# Author:   Andres Medina 
#-------------------------------------------------------------------------
# Last updated on : 13-December-2011
# 
#-------------------------------------------------------------------------

# path to binaries
TAR=/bin/tar

#                FUNCTIONS

# makes a backup of the whole directory
#-------------------------------------------------------------------------
full_backup(){  
  # set the backup directory, date and the zip file name
  BACKUP_DIR=$2
  DATE=$(date +%F)
  ZIP_NAME=$2.$DATE.$1
  WORKING_DIR=$(pwd)

  # check if the backup directory exist and if it's a directory
  if [ ! -d $WORKING_DIR/$BACKUP_DIR ]; then
    echo "ERROR: The directory $BACKUP_DIR doesn't exist or it's not a directory"
    exit 1;
  fi
  
  # create the compress file <archive.tarz> with the format directory_name.date.(f|i).tarz
  $TAR czf $ZIP_NAME.tarz $BACKUP_DIR
  echo "$ZIP_NAME.tarz create successfully";

}

# makes a incremental backup of the directory
#------------------------------------------------------------------------- 
partial_backup(){  
  # set the backup directory, date and the zip file name
  BACKUP_DIR=$2
  DATE=$(date +%F)
  ZIP_NAME=$2.$DATE.$1  
  WORKING_DIR=$(pwd)

  # check if the backup directory exist and if it's a directory
  if [ ! -d $WORKING_DIR/$BACKUP_DIR ]; then
    echo "ERROR: The directory $BACKUP_DIR doesn't exist or it's not a directory"
    exit 1;
  fi

  # create the compress file <archive.tarz> with the format directory_name.date.(f|i).tarz, for files  with modification date less than 1 week ago
  $TAR czf $ZIP_NAME.tarz -N "$(date -d '7 day ago')" $BACKUP_DIR
  echo "$ZIP_NAME.tarz create successfully"

}


# recover the files in the backup file <archive.tarz>
#------------------------------------------------------------------------- 
recover_backup(){
echo a
}
  # set the working directory and the backup file
  BACKUP_FILE=$2
  RECOVER_FILE=$3
  

  # check if the recover file exist
  if [ ! -e ./$BACKUP_FILE ]; then
    echo "ERROR: The file $BACKUP_FILE doesn't exist"
    exit 1;
  fi

  # extract the content of the <archive.tarz> file in the working directory
  FILES_NUM=$($TAR ztf $BACKUP_FILE | grep /$RECOVER_FILE | wc -l)
  if  [ $FILES_NUM == 0 ]; then
    echo "File not found in the backup archive"
    exit 1;
  fi
  for FILES in $($TAR ztf $BACKUP_FILE | grep /$RECOVER_FILE); do
    if [ $FILES_NUM == 1 ]; then
      OLD=$(pwd)
      mkdir aux
      cd aux
      $TAR -xzf $OLD/$BACKUP_FILE $FILES
      mv $FILES $OLD/$RECOVER_FILE
      cd $OLD
      rm -r aux
    fi;
  done

  echo "File $RECOVER_FILE successfully extracted from backup file $BACKUP_FILE"



#              MAIN PROGRAM
#
#-----------------------------------------------------------------------

# calls the right function according to the flag given by the user
case $1 in 
  f) full_backup;;
  i) partial_backup;;
  r) recover_backup;;
  *) echo "ERROR: wrong syntaxis";;
esac
